package com.xpgk.mvc.intoPersonnel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.upload.UploadFile;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.user.User;

import net.sf.json.JSONObject;

public class IntoPersonnelController extends BaseController {

	public void index() {
		String cusIds = getPara("personnelIds");
	
		if (cusIds != null) {
			User user = User.dao.findById(cusIds);	
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("column", IntoPersonnel.column_userIds);
			String sql = getSqlByBeetl(IntoPersonnel.sqlId_column, param);
			IntoPersonnel personnel = IntoPersonnel.dao.findFirst(sql, cusIds);
			
			setAttr("user", user);
			setAttr("intoPersonnel", personnel);
		
	}
		render("IntoPersonnel.html");
	}
		

	private IntoPersonnelServices intoPersonnelServices = new IntoPersonnelServices();

	/**
	 * 保存基本信息
	 */
	@Before(Tx.class)
	public void saveIntoPer() {
		String personnelIds = getPara("intoPersonnel.userIds");
		// 获取当前登录机构id
		String cudid = getCUserOrganIds();
		IntoPersonnel intoPersonnel = getModel(IntoPersonnel.class, "intoPersonnel");
		RenderBean result = intoPersonnelServices.saveIntoPer(intoPersonnel, personnelIds, cudid);
		renderJson(result.getMap());
	}

	public void toUserInfo() {
		render("IntoPersonnel.html");
	}

	/**
	 * 保存工作经验
	 */
	@Before(Tx.class)
	public void saveExp() {
		// 获取当前登录机构id
//		String cudid = getCUserOrganIds();
//		List<IntoExperience> ieList = getModels(IntoExperience.class);
//		String msg = intoPersonnelServices.saveExp(ieList, cudid);
//		renderText(msg);
		IntoExperience intoExperience = getModel(IntoExperience.class,"intoExperience");
		
		//IntoPersonnel intoPersonnel = getModel(IntoPersonnel.class, "intoPersonnel");
		RenderBean result = intoPersonnelServices.saveExp(intoExperience);
		renderJson(result.getMap());

	}

	/**
	 * 保存直系亲属
	 */
	@Before(Tx.class)
	public void saveRel() {
		IntoRelationship intoRelationship = getModel(IntoRelationship.class,"intoRelationship");

		RenderBean result = intoPersonnelServices.saveRel(intoRelationship);
		renderJson(result.getMap());
	}

	/**
	 * 图片上传
	 */
	public void imageUpload() {
		List<UploadFile> files = getFiles();

		// UploadFile uploadFile=getFile("zmfile");
		String cusIds = getPara("personnelIds");

		JSONObject json = intoPersonnelServices.imageUpload(files, cusIds);
		renderJson(json);
	}
	
	/**
	 * 工作经验列表
	 */
	public void personnelList() {
		
		String personnelIds = getPara("personnelIds");
		
		String sql = getSqlMy(IntoExperience.sqlId_selIntoExpByUserId);
		List<IntoExperience> exps = IntoExperience.dao.find(sql, personnelIds);
		System.out.println("11110000"+exps.toString());
		renderJson(exps);
	}
	
	/**
	 * 工作经验列表
	 */
	public void relList() {
		
		String personnelIds = getPara("personnelIds");
		
		String sql = getSqlMy(IntoRelationship.sqlId_selIntoRelByUserId);
		List<IntoRelationship> exps = IntoRelationship.dao.find(sql, personnelIds);
		
		renderJson(exps);
	}
}
